package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.OutboundDao;
import cn.gson.crm.model.dao.WaouDetailsDao;
import cn.gson.crm.model.mapper.OutboundMapper;
import cn.gson.crm.model.mapper.StockaMapper;
import cn.gson.crm.model.mapper.UserMapper;
import cn.gson.crm.model.pojos.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class OutboundService {
    @Autowired
    WaouDetailsDao waouDetailsDao;
    @Autowired
    OutboundDao outboundDao;
    @Autowired
    OutboundMapper outboundMapper;
    @Autowired
    StockaMapper stockaMapper;
    @Autowired
    UserMapper userMapper;
    //通用方法查询当前登录人是否为老板
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=userMapper.selectctlaofou(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return null;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }
    //新增
    public void updatsave(Outbound outbound){
        outboundDao.save(outbound);
    }
    public void save(Outbound outbound){
        outboundDao.save(outbound);
        outbound.getWaouDetailsList().forEach(p->{
            p.setOutbound(outbound);
        });
        waouDetailsDao.saveAll(outbound.getWaouDetailsList());
        if(outbound.getOuId()<=0){
            List<WaouDetails> waouDetails=outboundMapper.selectchu((int) outbound.getOuId());
            //判断如果库存中没有此产品就不减库存，（保险起见）
            if(!waouDetails.isEmpty()){
                for(int i=0;i<waouDetails.size();i++){
                    List<StockDetails> stockDetails=stockaMapper.selectkucun(waouDetails.get(i).getWoColor(),waouDetails.get(i).getWoCompany(),waouDetails.get(i).getWoSize(),waouDetails.get(i).getWoName());
                    if(!stockDetails.isEmpty()){
                        stockaMapper.updatekucun(stockDetails.get(0).getsNumber()-waouDetails.get(i).getWoNumber(),stockDetails.get(0).getStdeltId());
                    }
                }
            }
        }
    }
    //查询当前登录人查询所有出库单
    public Map<String,Object> selectck(HttpSession session,Integer yid,Integer page,Integer size){
        yid=fouyuan(session);
        List<Outbound> outbounds2=outboundMapper.selectck(yid);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Outbound> outbounds=outboundMapper.selectck(yid);
        List<DeliveryPlan> deliveryPlans=outboundMapper.selectjiaofu();
        Map<String,Object> map = new HashMap<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        map.put("total",obj.getTotal());
        map.put("rows",outbounds);
        map.put("jioafu",deliveryPlans);
        map.put("number","CK"+newDate+result);
        map.put("tong",outbounds2);
        return map;

    };
    //时间查询
    public Map<String,Object> selecttime(HttpSession session,Integer yid,  String statr,  String endtime,Integer page,Integer size){
        yid=fouyuan(session);
        List<Outbound> outbounds2=outboundMapper.selecttime(yid,statr,endtime);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Outbound> outbounds=outboundMapper.selecttime(yid,statr,endtime);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",outbounds);
        map.put("tong",outbounds2);
        return map;
    };
    //条件查询
    public Map<String,Object> selectwhere(HttpSession session,Integer yid, String where,Integer page,Integer size){
        yid=fouyuan(session);
        List<Outbound> outbounds2=outboundMapper.selectwhere(yid,where);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Outbound> outbounds=outboundMapper.selectwhere(yid,where);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",outbounds);
        map.put("tong",outbounds2);
        return map;
    };
    //修改状态
    public void updateout(Integer oid){
        outboundMapper.updateout(oid);
    };

    //减库存数量
    public void updatekucun(Integer oid){
        List<WaouDetails> waouDetails=outboundMapper.selectchu(oid);
        //判断如果库存中没有此产品就不减库存，（保险起见）
        if(!waouDetails.isEmpty()){
            for(int i=0;i<waouDetails.size();i++){
                List<StockDetails> stockDetails=stockaMapper.selectkucun(waouDetails.get(i).getWoColor(),waouDetails.get(i).getWoCompany(),waouDetails.get(i).getWoSize(),waouDetails.get(i).getWoName());
                if(!stockDetails.isEmpty()){
                    stockaMapper.updatekucun(stockDetails.get(0).getsNumber()-waouDetails.get(i).getWoNumber(),stockDetails.get(0).getStdeltId());
                }
            }
        }

    }
}
